package com.usf.jprovider.schema.connector;

import java.sql.Connection;
import java.sql.SQLException;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSeeAlso;

import com.usf.jprovider.schema.AbstractObject;
import com.usf.jprovider.tools.DAOTools;

@XmlSeeAlso({MysqlConnector.class, 
			 OracleConnector.class,
			 SQLServerConnector.class})
public abstract class AbstractConnector extends AbstractObject{

	@XmlElement protected String driver;
	@XmlElement protected String url;
	@XmlElement protected String user;
	@XmlElement protected String password;
	
	protected AbstractConnector() {
		super();
		driver = "";
		url = "";
		user = "";
		password ="";
	}
	protected AbstractConnector(String driver) {
		super();
		this.driver = driver;
	}
	public String getDriver() {
		return driver;
	}
	public String getUrl() {
		return url;
	}
	public String getUser() {
		return user;
	}
	public String getPassword() {
		return password;
	}	

	public String toString() {
		return "AbstractDatabaseConnector [driver=" + driver + ", url=" + url
				+ ", user=" + user + ", password=" + password + "]";
	}

	public final Connection connection() throws ClassNotFoundException, SQLException{
		return DAOTools.createConnection(driver, url, user, password);
	}
	
	public abstract String FileSourceFormater(String value) throws Exception;	
	public abstract String IntegerFormater(String value) throws Exception;
	public abstract String DoubleFormater(String value) throws Exception;
	public abstract String StringFormater(String value) throws Exception;
	public abstract String DateFormater(String date, String formater)  throws Exception;
	
	public String selectFirst(String table, String column) {
		return "SELECT DISTINCT " + column + " FROM " + table +
				" ORDER BY 1 LIMIT 1";
	}
	public String selectLast(String table, String column) {
		return "SELECT DISTINCT " + column + " FROM " + table +
				" ORDER BY 1 DESC LIMIT 1";
	}
	public String selectFirst(String query){
		return query + " ORDER BY 1 LIMIT 1";
	}
	public String selectLast(String query){
		return query + " ORDER BY 1 DESC LIMIT 1";
	}
	public abstract String selectRandomize(String table, String column);
	public abstract String selectRandomize(String table, String column, String queryValue);
	public abstract String selectRandomize(String query);
	public abstract String nextSequenceQuery(String name);
	
}
